<?php
// +----------------------------------------------------------------------
// * Created by PhpStorm.
// +----------------------------------------------------------------------
// * Author: D H
// +----------------------------------------------------------------------
// * Date: 2017/9/13
// +----------------------------------------------------------------------
// * Time: 18:28
// +----------------------------------------------------------------------
namespace app\admin\logic;
use think\Cookie;
use think\Db;
use think\Session;
use tool\Common;
class Product extends Base{

    //根据状态码得到产品状态   商家通用
    public function getSellProduct(){
       /*$data = [
           'status' => 0,
           'productName' => '闪亮灭火器',
           'quality' => '2017-2019',
           'catID' => 14,
           'origin' => '上海',
           'brandID' => 1,
           'model' => 'm233300',
        ];*/
        $data = $this->data;
        //如果是系统后台登录 查看商品审核信息
        $where = [];
        if (isset($data['productName'])&&$data['productName']){
            $where['name']=  $data['productName'];
        }
        if (isset($data['quality'])&&$data['quality']){
            $where['quality']=  $data['quality'];
        }
        if (isset($data['catID'])&& $data['catID']){
            $where['catalogID']=  $data['catID'];
        }
        if (isset($data['origin'])&&$data['origin']){
            $where['origin']=  $data['origin'];
        }
        if (isset($data['brandID'])&&$data['brandID']){
            $where['brandID']=  $data['brandID'];
        }
        if (isset($data['model'])&&$data['model']){
            $where['model']=  $data['model'];
        }
        $user = Db::name('user')->field('roleID')->where('id',$this->getUserID())->find();
        $sql = Db::view('product',['id' => 'productID','name' => 'productName','model','price','checkMsg','quality'])
            ->view('product_content','titleImage','product.id = product_content.productID')
            ->where('product.status',$data['status'])
            ->where($where);
        if ($user['roleID'] == 3){
            $list = $sql->select();
                // ->where('product.sellerID',$this->getUserID())
        }else{
            $list = $sql->where('product.sellerID',$this->getUserID())->select();
        }
        foreach ($list as $k => $val){
            $list[$k]['titleImage'] = json_decode($val['titleImage'],true);
            $list[$k]['titleImage'] =  $list[$k]['titleImage'][0];
            if ($data['status'] == 0){
                $list[$k]['status'] = '待审核';
            }
            if ($data['status'] == 1){
                $list[$k]['status'] = '审核通过';
            }
            if ($data['status'] == -1){
                $list[$k]['status'] = '审核未通过';
            }
        }
        if ($list){
            return Common::rm(1,'操作成功',$list);
        }else{
            return Common::rm(-3,'暂时没有数据');
        }
    }

    //谦程得到产品状态
    public function getAuditProduct()
    {
        $list = Db::view('product',['id' => 'productID','name' => 'productName','model','price','quality','sellerID'])
            ->view('product_content','titleImage','product.id = product_content.productID')
            ->view('user','sellerName','user.id = product.sellerID')
            ->where('product.status',$this->data['status'])
            ->select();
        if (!$list) {
            return Common::rm(-3,'数据为空');
        }
        $ar = [];
        foreach ($list as $key => $val) {
            $ar[$val['sellerID']][] = $val;
        }
        $arr = [];
        foreach ($ar as $k => $v) {
            $arr[$k]['sellerName'] =  $v[0]['sellerName'];
            foreach ($v as $_k => $_v) {
                $arr[$k]['product'][$_k]['productID'] = $_v['productID'];
                $arr[$k]['product'][$_k]['productName'] = $_v['productName'];
                $arr[$k]['product'][$_k]['model'] = $_v['model'];
                $arr[$k]['product'][$_k]['price'] = $_v['price'];
                $arr[$k]['product'][$_k]['quality'] = $_v['quality'];
                $arr[$k]['product'][$_k]['titleImage'] = json_decode($_v['titleImage'],true)[0];
            }
        }
        $arr = array_reverse($arr);
        return Common::rm(1,'操作成功',[
            'total' => count($list),
            'product' => $arr
        ]);
    }

    //修改产品信息 (审核未通过产品)
    public function updateProduct(){
        $data =  $this->data;
        //更新数据进product表的数据
        $arr = [
            'sellerID' => $this->getUserID(),        // 用户登录以后获取
            'catalogID' => $data['catalogID'],
            'name' => $data['name'],
            'model' => $data['model'],
            'manufacturer' => $data['manufacturer'],
            'origin' => $data['origin'],
            'class' => $data['class'],
            'price' => $data['price'],
            'status' => 0,                           //更新产品状态
            'addtime' => THINK_START_TIME
        ];
        //第一步判断品牌是否存在 存在返回ID 不存在添加返回ID
        $exist = Db::name('brand')->where('name',$data['brandName'])->find();
        if ($exist){
            $arr['brandID'] = $exist['id'];
        }else{
            $brandID = Db::name('brand')->insertGetId(['name' => $data['brandName']]);
            $arr['brandID'] = $brandID;
        }
        //更新数据进数据库
        Db::table('a_product')->where('id',$data['productID'])->where('sellerID',$this->getUserID())->update($arr);
        //更新数据进内容表
        $details = json_encode($data['details']);
        $titleImage = json_encode($data['titleImage']);
        $parameter = json_encode($data['parameterList']);
        $content = [
            //'productID' => $data['parentID'],              //产品ID 添加产品时获取
            'details' => $details,
            'titleImage' => $titleImage,
            'parameter' => $parameter
        ];
        Db::table('a_product_content')->where('id',$data['productID'])->update($content);
        return Common::rm(1,'操作成功');
    }

    //得到产品信息
    public function getUpProductMsg(){
       /* $data = [
            'productID' => 1
        ];*/
        $data = $this->data;
        $list = Db::view('product')
            ->view('product_content','details,titleImage,parameter','product.id = product_content.productID')
            ->view('cat',['name' => 'catName',],'cat.id = product.catalogID')
            ->view('brand',['name' => 'brandName',],'brand.id = product.brandID')
            ->view('user',['sellerName','logo','address','mobile','contactman'],'user.id = product.sellerID')
            ->where('product.id',$data['productID'])
            ->find();
        $newList = [
            'productID' => $list['id'],
            'catalogID' => $list['catalogID'],
            'brandName' => $list['brandName'],
            'name' => $list['name'],
            'model' => $list['model'],
            'manufacturer' => $list['manufacturer'],
            'origin' => $list['origin'],
            'class' => $list['class'],
            'number' => $list['number'],
            'weight' => $list['weight'],
            'price' => $list['price'],
            'quality' => $list['quality'],
            'details' => json_decode($list['details'],true),
            'titleImage' => json_decode($list['titleImage'],true),
            'parameter' => json_decode($list['parameter'],true),
        ];
        if ($list){
            return Common::rm(1,'操作成功',$newList);
        }else{
            return Common::rm(-2,'数据为空');
        }

    }

    //订单完成商品评价    只有购买该产品才有资格评价商品
    public function productEvaluation(){
        $data = $this->data;
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $exist = Db::name('shopping_cart')
            ->where('productID',$this->data['productID'])
            ->where('teamID',$this->getUserID())
            ->find();
        if (!$exist){
            return Common::rm(-1,'没有购买的商品您不能评价');
        }
        $data['addtime'] = THINK_START_TIME;
        $data['userID'] = $this->getUserID();
        $id = Db::name('product_comments')->insertGetId($data);
        Db::table('a_shopping_cart')
            ->where('productID',$this->data['productID'])
            ->where('teamID',$this->getUserID())
            ->update(['status' => 5]);
        if ($id > 0){
            return Common::rm(1,'评价成功');
        }else{
            return Common::rm(-2,'评价失败');
        }
    }

    //得到未评价商品(商家)  status = 4  未调用
    public function SNotAppraise(){
        $data = [
            'status' => 4,
           // 'sellerID' => 1,
            'sellerID' => $this->getUserID()
        ];
       $list =  $this->productInGeneral($data);
       if ($list){
           return Common::rm(1,'操作成功',$list);
       }else{
           return Common::rm(-2,'操作失败');
       }
    }

    //得到已评价商品(商家)  status = 5  未调用
    public function SAppraise(){
        $data = [
            'status' => 5,
            //'sellerID' => 5,
            'sellerID' => $this->getUserID()
        ];
        $list =  $this->productInGeneral($data);
        if ($list){
            return Common::rm(1,'操作成功',$list);
        }else{
            return Common::rm(-2,'操作失败');
        }
    }

    //得到未评价商品(买家)  status = 4  未调用
    public function notAppraise(){
        $data = [
            'status' => 4,
            //'teamID' => 13
            'teamID' => $this->getUserID()
        ];
        $list =  $this->productInGeneral($data);
        if ($list){
            return Common::rm(1,'操作成功',$list);
        }else{
            return Common::rm(-2,'操作失败');
        }
    }

    //得到已评价商品(买家)  status = 5  未调用
    public function appraise(){
        $data = [
            'status' => 5,
            //'teamID' => 1
            'teamID' => $this->getUserID()
        ];
        $list =  $this->productInGeneral($data);
        if ($list){
            return Common::rm(1,'操作成功',$list);
        }else{
            return Common::rm(-2,'操作失败');
        }
    }

    //谦程得到待审核产品信息
    public function getWaitProductMsg(){
       /* $data = [
            'productID' => 1
        ];*/
        $data = $this->data;
        $list = Db::view('product')
            ->view('product_content','details,titleImage,parameter','product.id = product_content.productID')
            ->view('cat',['name' => 'catName',],'cat.id = product.catalogID')
            ->view('brand',['name' => 'brandName',],'brand.id = product.brandID')
            ->view('user',['sellerName','logo','address','mobile','contactman'],'user.id = product.sellerID')
            ->where('product.id',$data['productID'])
            ->find();
        if (!$list){
            return Common::rm(-2,'不存在该产品');
        }
        $str = '';
        if ($list['class'] == 0 ){
            $str = '国产';
        }
        if ($list['class'] == 1){
            $str = '进口';
        }
        $newList = [
            'basicsName' =>'商品基本信息',
            'basicsList' =>[
                'productName' => $list['name'],
                'catName' => $list['catName'],
                'number' => $list['number'],
                'price' => $list['price'],
                'class' => $str
            ],
            'parameterName' => '商品属性详情',
            'parameter' =>[
                'brandName' => $list['brandName'],
                'model' => $list['model'],
                'origin' => $list['origin'],
                'manufacturer' => $list['manufacturer'],
                'weight' => $list['weight'],
                'quality' => $list['quality'],
                'parameter' => json_decode($list['parameter'],true)
            ],
            'imgName' => '商品图片信息',
            'imgList' => json_decode($list['titleImage']),
            'detail' => '商品详情信息',
            'detailList' => json_decode($list['details'],true)
        ];
        return Common::rm(1,'操作成功',$newList);
    }

    //得到分类列表
    public function getCatList(){
        $catList = getCatList();
        return Common::rm(1,'操作成功',$catList['content']);
    }

    //供应商得到总队审核产品列表
    public function getItemProduct(){
        $userID = $this->getUserID();
        $list = Db::view('team_select','sellerID')
           ->view('team_select_item','productID','team_select_item.selectID = team_select.id')
           ->view('product',['name' => 'productName','model','price','quality'],'team_select_item.productID = product.id')
           ->view('product_content','titleImage','product.id = product_content.productID')
           ->where('product.sellerID',$userID)
           ->select();
        foreach ($list as $k => $val){
            $list[$k]['titleImage'] = json_decode($val['titleImage'],true);
            $list[$k]['titleImage'] =  $list[$k]['titleImage'][0];
        }
        if ($list){
            return Common::rm(1,'操作成功',$list);
        }else{
            return Common::rm(-3,'暂时没有数据');
        }
    }

    //供应商店铺
    public function sellerShop(){
       /* $data = [
            'sellerID' => 1
        ];*/
        $data = $this->data;
        //判断是否传值 传值的话就是买家查看 不传就是商家自己查看
        if(isset($data['sellerID'])){
            $data['sellerID'] = $this->data['sellerID'];
        }else{
            $data['sellerID'] = $this->getUserID();
        }
        //判断该公户是否是供应商
        $exist = Db::name('team_select')->where('sellerID',$data['sellerID'])->find();
        if (!$exist){
            return Common::rm(-2,'该用户不是供应商');
        }
        //得到商品列表
        $List = Db::view('team_select','sellerID')
            ->view('team_select_item','productID','team_select.id = team_select_item.selectID')
            ->view('product',['id' => 'productID','name' => 'productName','price','catalogID'],'team_select_item.productID = product.ID')
            ->view('user','sellerName','product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product_content.productID = product.id   ')
            ->where('product.sellerID',$data['sellerID'])
            ->select();
        $arr = $this->shop($List);
        return Common::rm(1,'操作成功',$arr['content']);
    }

    //+---------------------------------------------------
    //*卖家卖家评价未评价公有调用
    //+---------------------------------------------------
    public function productInGeneral($data){
        $con = ''; $use = '';
        foreach ($data as $k => $val){
            if ($k == 'sellerID'){
                $con = 'sellerID';
                $use = 'teamID';
                break;
            }else{
                $con = 'teamID';
                $use = 'sellerID';
            }
        }
        //得到商家/支队/总队 已经卖出的商品
        $list = Db::view('product',['id' => 'productID','name' => 'productName','model'])
            ->view('brand','name','product.brandID = brand.id')
            ->view('product_content','titleImage','product.id = product_content.productID')
            ->view('shopping_cart','number,total','product.id = shopping_cart.productID')
            ->view('user','sellerName','user.id = shopping_cart.'.$use.'')
            ->where('shopping_cart.'.$con.'',$data[$con])
            ->where('shopping_cart.status',$data['status'])   //用以判断商品是否被评价
            ->select();
        $img =[];
        $id = [];
        foreach ($list as $k => $val){
            $img = json_decode($val['titleImage']);
            $list[$k]['img'] = $img[0];
            $id[] = $val['productID'];
            unset($list[$k]['titleImage']);
        }
        //根据合同返回样式
        $arr = Db::name('contract')->field('name,number,productIDS,addtime')->where($con,$data[$con])->select();
        $array = [];
        $new_arr = [];
        foreach ($arr as $k => $val){
            $array = json_decode($val['productIDS'],true);
            $arr[$k]['addtime'] = date('Y-m-d',$val['addtime']);
            unset($arr[$k]['productIDS']);
            foreach ($list as $key => $value){
                $arr[$k]['msg']['sellerName'] = $value['sellerName'];
                $arr[$k]['msg']['total'] = $value['total'];
                if ($data['status'] == 4){
                    $arr[$k]['msg']['status'] = '已收货';
                    $arr[$k]['msg']['con'] = '未评价';
                }else{
                    $arr[$k]['msg']['status'] = '已收货';
                    $arr[$k]['msg']['con'] = '已评价';
                }
                if (in_array($value['productID'],$array)){
                    $new_arr = [
                        'productID' => $value['productID'],
                        'name' => $value['name'].'/'.$value['productName'],
                        'model' => $value['model'],
                        'img' => $value['img'],
                    ];
                    $arr[$k]['productList'][] =  $new_arr;
                }
            }
        }
        //return $array;
        return $arr;
    }

    //+---------------------------------------------------
    //*店铺公有调用
    //+---------------------------------------------------
    public function shop($List){
        $proList = [];
        $proIDS = [];
        $catIDS = [];
        $str = '';
        //产品列表
        foreach ($List as $k => $val){
            $proList[$k]['titleImage'] = json_decode($val['titleImage']);
            $proList[$k]['titleImage'] = $proList[$k]['titleImage'][0];
            $proList[$k]['name'] = $val['brandName'].'/'.$val['productName'];
            $proList[$k]['sellerName'] = $val['sellerName'];
            $proList[$k]['price'] = $val['price'];
            $proList[$k]['productID'] = $val['productID'];
            //产品ID
            $proIDS[$k] = $val['brandID'];
            //分类ID
            $catIDS[$k] = $val['catalogID'];
            //店铺名称
            $str = $val['sellerName'];
        }
        //品牌列表
        $brandList = Db::name('brand')
            ->field(['id' => 'brandID','name' => 'brandName'])
            ->where('id','in',$proIDS)
            ->distinct(true)
            ->select();
        //分类列表
        $catList = Db::name('cat')
            ->field(['id' => 'catID','name' => 'catName'])
            ->where('id','in',$catIDS)
            ->distinct(true)
            ->select();

        return Common::rm(1,'操作成功',[
            'shopName' => $str,
            'brandList' => $brandList,
            'catList' => $catList,
            'productList' => $proList
        ]);
    }

}